import java.util.ArrayList;
import java.util.Collections;
import java.util.NoSuchElementException;


public class Matrix {

	protected ArrayList<Row> values;
	protected int rowSize;
	protected int columnSize;
		
	public Matrix(int rows, int columns)
	{
		values = new ArrayList<Row>(rows);
		for(int i = 0; i < rows; i++)
			values.add(new Row(columns, this));
		rowSize = rows;
		columnSize = columns;
	}
		
	public void set(int i, int j, double value)
	{
		Row row = null;
		
		if(i >= values.size())
			throw new NoSuchElementException();
		row = values.get(i);
		
		row.set(j, value);
	}
	
	public double getValue(int i, int j)
	{
		Row row = null;
		
		if(i >= values.size())
			throw new NoSuchElementException();
	
		row = values.get(i);
		
		return row.get(j);
	}
	
	public int getRowSize()
	{
		return rowSize;
	}
	
	public int getColumnSize()
	{
		return columnSize;
	}
	
	public MatrixRowIterator getRowIterator(int i)
	{
		return new MatrixRowIterator(values.get(i));
	}
	
	public MatrixColumnIterator getColumIterator(int j)
	{
		return new MatrixColumnIterator(this, j);
	}
	
	public Double getMinColumValue(int j)
	{
		MatrixColumnIterator iterator = this.getColumIterator(j);
		Double min = new Double(Double.MAX_VALUE);
		
		while(iterator.hasNext())
		{
			Double current = iterator.next();
			
			if(current < min)
				min = current;
		}
		return min;
	}
	
	public Double getMaxColumValue(int j)
	{
		MatrixColumnIterator iterator = this.getColumIterator(j);
		Double min = new Double(Double.MIN_VALUE);
		
		while(iterator.hasNext())
		{
			Double current = iterator.next();
			
			if(current > min)
				min = current;
		}
		return min;
	}
	
	public void sort()
	{
		Collections.sort(values);
	}
}
